using System;
using System.Configuration;
using System.Data;
using digit.Core;

namespace SMS.ServiceManager
{
    public class ActivitiesManager
    {
        #region CONSTRUCTORS
        static ActivitiesManager()
        {
            _data = new ActivitiesData(Util.GetConnectionString("SMSSERVICE"));
            _error = "";
        }
        #endregion

        #region ATTRIBUTES
        private static ActivitiesData _data;
        private static string _error;
        #endregion

        #region PROPERTIES
        public static string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        #endregion

        #region METHODS
        public static bool Insert(string tableName, int userID, string action, string doContent, DateTime activityDate)
        {
            _error = "";
            try
            {
                object[] values = new object[5];
                values[0] = Util.ReplaceNullTo(tableName, DBNull.Value);
                values[1] = userID;
                values[2] = Util.ReplaceNullTo(action, DBNull.Value);
                values[3] = Util.ReplaceNullTo(doContent, DBNull.Value);
                values[4] = Util.ReplaceNullTo(activityDate, DBNull.Value);

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Insert(ActivitiesInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[5];
                values[0] = Util.ReplaceNullTo(info.TableName, DBNull.Value);
                values[1] = info.UserID;
                values[2] = Util.ReplaceNullTo(info.Action, DBNull.Value);
                values[3] = Util.ReplaceNullTo(info.DoContent, DBNull.Value);
                values[4] = Util.ReplaceNullTo(info.ActivityDate, DBNull.Value);

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(long logID, string tableName, int userID, string action, string doContent, DateTime activityDate)
        {
            _error = "";
            try
            {
                object[] values = new object[6];
                values[0] = logID;
                values[1] = Util.ReplaceNullTo(tableName, DBNull.Value);
                values[2] = userID;
                values[3] = Util.ReplaceNullTo(action, DBNull.Value);
                values[4] = Util.ReplaceNullTo(doContent, DBNull.Value);
                values[5] = Util.ReplaceNullTo(activityDate, DBNull.Value);

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(ActivitiesInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[6];
                values[0] = info.LogID;
                values[1] = Util.ReplaceNullTo(info.TableName, DBNull.Value);
                values[2] = info.UserID;
                values[3] = Util.ReplaceNullTo(info.Action, DBNull.Value);
                values[4] = Util.ReplaceNullTo(info.DoContent, DBNull.Value);
                values[5] = Util.ReplaceNullTo(info.ActivityDate, DBNull.Value);

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(long logID)
        {
            _error = "";
            try
            {
                object[] values = { logID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(ActivitiesInfo info)
        {
            _error = "";
            try
            {
                object[] values = { info.LogID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static ActivitiesInfo Select(long logID)
        {
            _error = "";
            try
            {
                object[] values = { logID };
                DataTable dTable = _data.Select(values);
                if (dTable.Rows.Count < 1) return null;


                ActivitiesInfo info = new ActivitiesInfo(dTable.Rows[0]);
                return info;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAll()
        {
            _error = "";
            try
            {
                return _data.SelectAll();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable ListBy(string table, long userId)
        {
            _error = "";
            try
            {
                object[] values = {table, userId};
                return _data.ListBy(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        #endregion

    }
}